import requests
import json
from DrissionPage import SessionPage, ChromiumOptions, SessionOptions, Chromium
from DrissionPage.common import Keys

DETAIL_URL = "https://ad.xiaohongshu.com/api/leona/rtb/unit/detail"
headers = {
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9",
    "content-type": "application/json",
    "origin": "https://ad.xiaohongshu.com",
    "priority": "u=1, i",
    "referer": "https://ad.xiaohongshu.com/aurora/ad/edit/unit/119873812/262477246/4/1?byManage=true&manageLevel=byAccount&AFormGrayFlag=false&vSellerId=643e0c60c8fa340001aea24c",
    "sec-ch-ua": "\"Chromium\";v=\"136\", \"Google Chrome\";v=\"136\", \"Not.A/Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36",
    "v-seller-id": "643e0c60c8fa340001aea24c",
    "x-b3-traceid": "c9d409c4f105bcb6"
}

def open_account_palt_form(account_id, result_text=None):
    co = ChromiumOptions(read_file=False)  # 不读取文件方式新建配置对象
    so = SessionOptions(read_file=False)
    browser = Chromium(addr_or_opts=co, session_options=so)
    account_tab = browser.new_tab('https://partner.xiaohongshu.com/partner/subAccount-list')

    # 加一个校验步骤， 不是叁柒的所属邮箱直接驳回
    # account_tab.ele('tag:span@@class=d-text --color-static --color-text-paragraph --size-text-paragraph text label@@text()^登陆邮箱：').next().child().next().click(by_js=True)
    # account_tab.wait(0.5)
    # email_str = account_tab.ele('tag:span@@class=d-text --color-static --color-text-paragraph --size-text-paragraph text label@@text()^登陆邮箱：').next().child().text
    # email_permission_list = email_list(result_text)
    #
    # # 用户名 ， 用于后面@人
    # username = account_tab.ele('tag:span@@class=d-text --color-static --color-text-paragraph --size-text-paragraph text label@@text()^用户名：').next().text
    # if email_str not in email_permission_list:
    #     log_message(result_text, f"当前邮箱账号:【{email_str}】暂无权限使用", "red")
    #     return

    account_tab.ele('tag:input@@class=el-input__inner@@type=text@@autocomplete=off@@placeholder=请输入').click()
    account_tab.ele('tag:input@@class=el-input__inner@@type=text@@autocomplete=off@@placeholder=请输入').input(account_id).input(Keys.ENTER)

    account_tab.wait(1)

    search_none_ele = account_tab.ele('tag:tr@@class=d-table__empty-row@@text()=暂无数据', timeout=0.5)
    if search_none_ele:
        return

    account_tab.ele('tag:span@@text():跳转', timeout=2).hover()
    jg_ele = account_tab.ele('tag:span@@class=d-text --color-static --color-current --size-text-paragraph d-text-nowrap d-text-ellipsis@@text():聚光平台', timeout=2)

    create_plan_tab = jg_ele.click.for_new_tab(by_js=True)
    create_plan_tab.wait.title_change("计划列表")
    unit_dict_cookie = create_plan_tab.cookies(all_info=False).as_dict()
    return unit_dict_cookie


def get_unit_detail(unit_id, account_id, unit_dict_cookie):
    """
    获取指定单元的原始数据
    :param account_id:
    :param unit_id: 单元ID
    :return: 原始数据 dict
    """
    headers['v-seller-id'] = account_id
    payload = {"id": unit_id}
    response = requests.post(DETAIL_URL, headers=headers, cookies=unit_dict_cookie, data=json.dumps(payload))

    if response.status_code == 200:
        return response.json().get('data', {})
    return {}
